package components
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.ComboBox;
	import mx.controls.Label;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class NewCourse{
		private var panel:SuperPanel = new SuperPanel();
		private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var tool:Tool = new Tool();
		private var labelNaziv:Label = new Label();
		private var labelSifra:Label = new Label();
		private var labelOpis:Label = new Label();
		private var labelECTS:Label = new Label();
		private var labelSmjer:Label = new Label();
		private var labelObavezan:Label = new Label();
		private var comboBoxSmjer:ComboBox = new ComboBox();
		private var comboBoxObavezan:ComboBox = new ComboBox();
		private var nazivTextField:TextInput = new TextInput();
		private var sifraTextField:TextInput = new TextInput();
		private var opisTextArea:TextArea = new TextArea();
		private var ectsTextField:TextInput = new TextInput();
		private var btn:Button = new Button();
		
		public function NewCourse(p:SuperPanel){
			panel = p;
			labelNaziv.x = 10;
			labelNaziv.y = 27;
			labelNaziv.width = panel.width-30;
			labelNaziv.height = 30;
			labelNaziv.text = "Naziv kursa: ";
			panel.addChild(labelNaziv);
			
			labelSifra.x = 10;
			labelSifra.y = 47;
			labelSifra.width = panel.width-30;
			labelSifra.height = 30;
			labelSifra.text = "Sifra: ";
			panel.addChild(labelSifra);
			
			nazivTextField.x = 110;
			nazivTextField.y = 27;
			nazivTextField.width = panel.width-130;
			nazivTextField.height = 18;
			panel.addChild(nazivTextField);
			
			sifraTextField.x = 110;
			sifraTextField.y = 47;
			sifraTextField.width = panel.width-130;
			sifraTextField.height = 18;
			panel.addChild(sifraTextField);
							
			labelECTS.x = 10;
			labelECTS.y = 67;
			labelECTS.width = panel.width-30;
			labelECTS.height = 30;
			labelECTS.text = "ECTS kredita: ";
			panel.addChild(labelECTS);
			
			labelObavezan.x = 10;
			labelObavezan.y = 87;
			labelObavezan.width = panel.width-30;
			labelObavezan.height = 30;
			labelObavezan.text = "Obavezan: ";
			panel.addChild(labelObavezan);

			labelSmjer.x = 10;
			labelSmjer.y = 107;
			labelSmjer.width = panel.width-30;
			labelSmjer.height = 30;
			labelSmjer.text = "Smjer: ";
			panel.addChild(labelSmjer);
			
			labelOpis.x = 10;
			labelOpis.y = 127;
			labelOpis.width = panel.width-30;
			labelOpis.height = 30;
			labelOpis.text = "Opis: ";
			panel.addChild(labelOpis);
			
			ectsTextField.x = 110;
			ectsTextField.y = 67;
			ectsTextField.width = panel.width-130;
			ectsTextField.height = 18;
			panel.addChild(ectsTextField);
			
			comboBoxObavezan.x = 110;
			comboBoxObavezan.y = 87;
			comboBoxObavezan.width = panel.width-130;
			comboBoxObavezan.height = 18;
			panel.addChild(comboBoxObavezan);

			comboBoxSmjer.x = 110;
			comboBoxSmjer.y = 107;
			comboBoxSmjer.width = panel.width-130;
			comboBoxSmjer.height = 18;
			panel.addChild(comboBoxSmjer);
			
			opisTextArea.x = 110;
			opisTextArea.y = 127;
			opisTextArea.width = panel.width-130;
			opisTextArea.height = 28;
			panel.addChild(opisTextArea);
					
			btn.x = 10;
			btn.y = 157;
			btn.width = panel.width-30;
			btn.height = 18;
			btn.label = "Dodaj kurs";
			panel.addChild(btn);
			
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
			btn.addEventListener(MouseEvent.CLICK, onClick);
			
			service1.sql = {type:"SELECT", a1:"s.id_smjer", a2:"s.naziv", from:"smjer s", where:"" };
	 		service1.addEventListener(service1.MYSQL_RESULT, onResultSmjer);
	 		service1.startService();
		}
		
		public function onResultSmjer(e:Event):void{
			var xmlList:XMLList = service1.xml.children();
			var xmlPom:String = "<root naziv='Smjerovi' tip='-1'>";
			for (var i:int; i<xmlList.length(); i++) {
				xmlPom+="<smjer id='"+xmlList[i].id_smjer+"' naziv='"+xmlList[i].naziv+"' />";
			}
			xmlPom+="</root>";
			var xml:XML=new XML(xmlPom);
			comboBoxSmjer.dataProvider = xml.children();
			comboBoxSmjer.labelField = "@naziv";
			
			var xmlPom1:String = "<root naziv='Obavezni' tip='-1'><value id='1' naziv='Da (obavezan predmet)'/><value id='0' naziv='Ne (izborni predmet)'/></root>";
			var xml1:XML=new XML(xmlPom1);
			comboBoxObavezan.dataProvider = xml1.children();
			comboBoxObavezan.labelField = "@naziv";
		}

		public function onResize(e:Event):void{
			
		}
		
		public function onClick(e:MouseEvent):void{
			if(nazivTextField.text.length>0 && sifraTextField.text.length>0, ectsTextField.text.length>0 && opisTextArea.text.length>0){
            	service.sql = {type:"INSERT", into:"kurs", columns:"naziv, sifra, opis, ects, smjer, obavezan", values:"@"+nazivTextField.text+"@,@"+sifraTextField.text+"@,@"+tool.CodeTekst(opisTextArea.text)+"@,@"+ectsTextField.text+"@,@"+comboBoxSmjer.selectedItem.@id+"@,@"+comboBoxObavezan.selectedItem.@id+"@" }; 
				service.addEventListener(service.MYSQL_RESULT, onResult);
 				service.startService();
 			} else tool.createAlert("GRESKA", "Morate unijeti sva polja!");
		}
		
		public function onResult(e:Event):void{
			tool.createAlert("OK", "Kurs je uspjesno upisan u bazu!");
			panel.closePanel();
		}
	}
}